﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FMPCore;
using System.Collections.ObjectModel;

namespace Client.UserControls
{
    /// <summary>
    /// Interaction logic for ucContacts.xaml
    /// </summary>
    public partial class ucContacts : UserControl
    {
        public ucContacts()
        {
            InitializeComponent();
        }

        public delegate void delUpdateContactList(ObservableCollection<Contact> contactList);

        private void setContactList(ObservableCollection<Contact> contactList)
        {
            if (tvContacts.Dispatcher.Thread == System.Threading.Thread.CurrentThread)
            {
                tvContacts.ItemsSource = contactList;
            }
            else
            {
                tvContacts.Dispatcher.BeginInvoke(new delUpdateContactList(setContactList), System.Windows.Threading.DispatcherPriority.Normal, contactList);
            }
        }

        public List<Contact> SetContacts
        {
            set
            {
                if (value != null)
                {
                    ObservableCollection<Contact> contactList = new ObservableCollection<Contact>(value);
                    setContactList(contactList);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("{0} tried to set null contactList.", DateTime.Now));
                }
            }
        }

        public delegate void OpenChatEventHandler(object sender, string username);
        public event OpenChatEventHandler OpenChat;

        private void tvContacts_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is TreeView && OpenChat != null)
            {
                TreeView tv = sender as TreeView;
                Contact c = tv.SelectedItem as Contact;

                OpenChat(this, c.UserName);
            }
        }

        public event OpenChatEventHandler AddContact;

        private void miAddContact_Click(object sender, RoutedEventArgs e)
        {
            if (AddContact != null)
            {
                AddContact(this, null);
            }
        }

        public event OpenChatEventHandler RemoveContact;

        private void miRemoveContact_Click(object sender, RoutedEventArgs e)
        {
            if (RemoveContact != null && selectContact != null)
            {
                RemoveContact(this, selectContact.UserName);
                selectContact = null;
            }
        }

        Contact selectContact = null;

        /// <summary>
        /// When the user release the couse it will select the save what was selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvContacts_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (sender is TreeView)
            {
                TreeView tv = sender as TreeView;
                selectContact = tv.SelectedItem as Contact;
            }
        }

        /// <summary>
        /// Check to see when the right click is pressed what the user was trying to select
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvContacts_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;

            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
                e.Handled = true;
            }
        }

        /// <summary>
        /// Finds the TreeViewItem
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

        public event OpenChatEventHandler SendFileToUser;

        /// <summary>
        /// Send a file to a user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miSendFile_Click(object sender, RoutedEventArgs e)
        {
            if (SendFileToUser != null && selectContact != null)
            {
                SendFileToUser(this, selectContact.UserName);
            }
        }

    }
}
